package cn.iocoder.yudao.module.aiposter.service.poster;
import cn.iocoder.yudao.module.aiposter.controller.admin.poster.vo.PosterPageReqVO;
import cn.iocoder.yudao.module.aiposter.controller.admin.poster.vo.PosterSaveReqVO;
import cn.iocoder.yudao.module.aiposter.dal.dataobject.poster.PosterDO;
import cn.iocoder.yudao.module.aiposter.dal.mysql.poster.PosterMapper;
import cn.iocoder.yudao.module.aiposter.service.poster.PosterService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.aiposter.enums.ErrorCodeConstants.POSTER_NOT_EXISTS;

/**
 * 海报信息 Service 实现类
 *
 * @author Rain
 */
@Service
@Validated
public class PosterServiceImpl implements PosterService {

    @Resource
    private PosterMapper posterMapper;

    @Override
    public Long createPoster(PosterSaveReqVO createReqVO) {
        // 插入
        PosterDO poster = BeanUtils.toBean(createReqVO, PosterDO.class);
        posterMapper.insert(poster);
        // 返回
        return poster.getId();
    }

    @Override
    public void updatePoster(PosterSaveReqVO updateReqVO) {
        // 校验存在
        validatePosterExists(updateReqVO.getId());
        // 更新
        PosterDO updateObj = BeanUtils.toBean(updateReqVO, PosterDO.class);
        posterMapper.updateById(updateObj);
    }

    @Override
    public void deletePoster(Long id) {
        // 校验存在
        validatePosterExists(id);
        // 删除
        posterMapper.deleteById(id);
    }

    private void validatePosterExists(Long id) {
        if (posterMapper.selectById(id) == null) {
            throw exception(POSTER_NOT_EXISTS);
        }
    }

    @Override
    public PosterDO getPoster(Long id) {
        return posterMapper.selectById(id);
    }

    @Override
    public PageResult<PosterDO> getPosterPage(PosterPageReqVO pageReqVO) {
        return posterMapper.selectPage(pageReqVO);
    }

}